﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Frame.Lib.Core.Register;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;
using Frame.Lib.Core.Tool;
using Frame.Lib.Core.Config;
using Frame.Lib.Core.Cache;
using Frame.Lib.Core.Cache.SysConfig;


namespace Frame.Web.Admin.Tools
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                txtLocalMac.Text = NetworkAdapter.GetFirstMacAddr();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string loginName = txtLoginName.Text.Trim();
            string password = txtPassword.Text.Trim();
  
            if (AdminTool.Validate(loginName, password))
            {
                RegisterInfo info = new RegisterInfo();
                if (chkLong.Checked)
                {
                    info.ExpireDate = DateTime.MaxValue;
                }
                else
                {
                    info.ExpireDate = DateTime.Parse(txtExpireDate.Text);
                }
                info.Mac = txtMac.Text.Replace("，", ",").Trim();

                txtCode.Text = Serialize.BinarySerialize(info);
            }
            else
            {
                JSUtil.Alert("用户名或密码错");
                
            }
            
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            string loginName = txtLoginName.Text.Trim();
            string password = txtPassword.Text.Trim();

            if (AdminTool.Validate(loginName, password))
            {                
                SysConfigBO config = new SysConfigBO();
                SysConfigInfo cfgInfo = config.GetInfo(ConfigID.reginfo.ToString());
                if (cfgInfo != null)
                {
                    RegisterInfo info = Serialize.BinaryDeserialize(cfgInfo.Config) as RegisterInfo;

                    txtMac.Text = info.Mac;
                   
                    if (info.ExpireDate == DateTime.MaxValue)
                    {
                        chkLong.Checked = true;
                    }
                    else
                    {
                        txtExpireDate.Text = info.ExpireDate.ToString();

                    }

                }
            }
            else
            {
                JSUtil.Alert("用户名或密码错");

            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                //防止非法数据保存进去
                RegisterInfo info = (RegisterInfo)Serialize.BinaryDeserialize(txtCode.Text);


                SysConfigBO config = new SysConfigBO();
                SysConfigInfo cfgInfo = config.GetInfo(ConfigID.reginfo.ToString());
                bool isAdd = cfgInfo == null;
                cfgInfo = cfgInfo ?? new SysConfigInfo();
                cfgInfo.ID = ConfigID.reginfo.ToString();
                cfgInfo.Config = txtCode.Text;


                config.Save(cfgInfo, isAdd);

                CacheManager.Instance.Update(typeof(SysConfigLoader));
                JSUtil.Alert("保存成功");

            }
            catch (Exception ex)
            {
                JSUtil.Alert(ex.Message);
            }

        }
    }
}
